package com.edu.edu.day9_19_week7_homework.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TextInputEditText;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.edu.edu.day9_19_week7_homework.R;

import cn.bmob.v3.Bmob;
import cn.bmob.v3.BmobSMS;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.RequestSMSCodeListener;
import cn.bmob.v3.listener.SaveListener;
import cn.bmob.v3.listener.VerifySMSCodeListener;

/**
 * Created by Administrator on 9/19 0019.
 */
public class RegisterActivity extends AppCompatActivity implements View.OnClickListener{

    private TextInputEditText tel;
    private TextInputEditText passWord;
    private TextInputEditText code;
    private Button send;
    private Button register;
    private BmobUser mUser;
    private String phoneTel;
    private String password;
    private String mCode;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);//绑定注册页面布局
        initView();//初始化控件
    }

    private void initView() {
        tel = (TextInputEditText) findViewById(R.id.register_tel);
        passWord = (TextInputEditText) findViewById(R.id.register_userPassword);
        code = (TextInputEditText) findViewById(R.id.register_code);
        send = (Button) findViewById(R.id.register_send);
        register = (Button) findViewById(R.id.register_register);
        //按钮设置监听
        send.setOnClickListener(this);
        register.setOnClickListener(this);
        //第一：默认初始化
        Bmob.initialize(RegisterActivity.this, "fe5f0d457e1c81e0286d8f68e74a603d");
        // 使用BmobSDK提供的注册功能
        mUser = new BmobUser();
    }
    @Override
    public void onClick(View view) {
        //获取文本值
        phoneTel = tel.getText().toString().trim();
        password = passWord.getText().toString().trim();
        mCode = code.getText().toString().trim();

        switch (view.getId()){
            //获取验证码
            case R.id.register_send:
                //判断手机号位数
                if(phoneTel.length() != 11){
                    tel.setError("输入错误");
                    Toast.makeText(RegisterActivity.this, "请输入有效11位手机号!", Toast.LENGTH_SHORT).show();
                }else{
                    //获取验证码方法
                    BmobSMS.requestSMSCode(RegisterActivity.this, phoneTel, "短信模板", new RequestSMSCodeListener() {
                        @Override
                        public void done(Integer integer, BmobException e) {
                            //如果异常为空则发送成功
                            if(e == null){
                                send.setClickable(false);//按钮变为不可点击状态
                                send.setBackgroundColor(Color.GRAY);//换背景颜色
                                Toast.makeText(RegisterActivity.this, "验证码发送成功,请查收！", Toast.LENGTH_SHORT).show();
                            }else{
                                Toast.makeText(RegisterActivity.this, "验证码发送失败!", Toast.LENGTH_SHORT).show();

                            }
                        }
                    });
                }
                break;
            //注册按钮
            case R.id.register_register:
                // 非空验证
                if(phoneTel.length() == 0 || password.length() == 0 || phoneTel.length() != 11 || mCode.length() == 0){
                    Toast.makeText(this, "输入有误,请重新填写", Toast.LENGTH_SHORT).show();
                    tel.setError("输入错误");
                    code.setError("输入错误");
                    return;
                }else{
                    //核对验证码
                    BmobSMS.verifySmsCode(RegisterActivity.this, phoneTel, mCode, new VerifySMSCodeListener() {
                        @Override
                        public void done(BmobException e) {
                            //如果异常为空则注册成功
                            if(e == null){
                                //验证用户名跟密码是否正确
                                mUser.setUsername(phoneTel);
                                mUser.setPassword(password);
                                //利用singUp方法进行注册
                                mUser.signUp(RegisterActivity.this, new SaveListener() {
                                    @Override
                                    public void onSuccess() {
                                        //注册成功，跳转到登录界面登录
                                        Intent intent = new Intent(RegisterActivity.this,LoginActivity.class);
                                        //将每次注册的用户名跟密码传送到登陆页面
                                        intent.putExtra("phoneTel",phoneTel);
                                        intent.putExtra("password",password);
                                        startActivity(intent);//开启意图
                                        Toast.makeText(RegisterActivity.this, "注册成功,请登录.", Toast.LENGTH_SHORT).show();
                                    }
                                    @Override
                                    public void onFailure(int i, String s) {
                                        Toast.makeText(RegisterActivity.this, "注册失败", Toast.LENGTH_SHORT).show();
                                    }
                                });
                            }else{
                                Toast.makeText(RegisterActivity.this, "验证码有误!", Toast.LENGTH_SHORT).show();
                            }
                        }
                    });
                }
                break;
        }
    }
}
